package com.gelapps.memogel.cards;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gelapps.memogel.Constants;
import com.gelapps.memogel.MainActivity;
import com.gelapps.memogel.R;
import com.gelapps.memogel.credentials.CredentialManager;

import java.util.ArrayList;

/**
 * Created by matteolappa on 20/06/2013.
 */
public class CardAdapter extends ArrayAdapter<CardsMaster> {

    public CardAdapter(Activity activity, ArrayList<CardsMaster> array)
    {
        super(activity, 0, array);
    }

    @Override
      public View getView(int position, View convertView, ViewGroup parent)
    {
        Activity a = (Activity) getContext();
        LayoutInflater inflater = a.getLayoutInflater();

        View rowView = inflater.inflate(R.layout.card_row_main_activity, null);

        final CardsMaster card = getItem(position);
        final int pos = position;
        TextView textView = (TextView) rowView.findViewById(R.id.Card);
        textView.setText(card.getName());

        ((ImageView) rowView.findViewById(R.id.imgEdit)).setOnClickListener(new View.OnClickListener()
        {
            CardsMaster c = card;
            @Override
            public void onClick(View view) {
                Log.i("OnClickDelete", "Edit " + c.getName());
                Intent i= new Intent(getContext(), CardEdit.class);
                String key = ((Activity) getContext()).getIntent().getExtras().getString(Constants.getKeyString());
                Bundle bundle = new Bundle();
                bundle.putString(Constants.getCardsName(),CardsManager.getInstance().getCardIndex(pos).getName());
                bundle.putString(Constants.getKeyString(),key);
                i.putExtras(bundle);
                view.getContext().startActivity(i);
            }
        });

        ((ImageView) rowView.findViewById(R.id.imgDelete)).setOnClickListener(new View.OnClickListener()
        {
            CardsMaster c = card;
            @Override
            public void onClick(View view) {
                Log.i("OnClickDelete", "Delete " + c.getName());
                //CredentialManager.getInstance().delCredential(c.getName());
                AlertDialog.Builder delDialog = new AlertDialog.Builder(getContext());
                delDialog.setTitle(getContext().getResources().getString(R.string.strDelTitle) + c.getName() +" ?");
                delDialog.setMessage(getContext().getResources().getString(R.string.strDelMessage));
                delDialog.setPositiveButton(R.string.strYes,new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog,int which)
                    {
                        Log.i("OnclickDelete","YES delete");
                        delCard(pos);
                    }

                });
                delDialog.setNegativeButton(R.string.strNo, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.i("OnclickDelete", "NO delete");
                    }

                });

                delDialog.show();
            }
        });

        return rowView;
    }

    private void delCard(int pos)
    {
        CardsManager.getInstance().delCard(CardsManager.getInstance().getCardIndex(pos).getName());
        this.remove(CardsManager.getInstance().getCardIndex(pos));
        this.notifyDataSetChanged();
    }




}
